﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestionePolveriLib.Facades;
using BrightIdeasSoftware;
using GestionePolveriLib.BL;

namespace GestionePolveri2
{
    public partial class AssociaPolvereDialog : Form
    {
        private List<PolvereExt> m_polveri = null;
        private List<Colore> m_colori = null;
        private ComposizioneColore2 m_formula = null;
        private ComposizioneColoreExt dataExt = null;

        public ComposizioneColoreExt ComposizioneFormula
        {
            get { return dataExt; }
            set { dataExt = value; }
        }

        public ComposizioneColore2 Formula 
        {
            get { return m_formula; }
            set 
            { 
                m_formula = value;

                ModifyFormula(m_formula);
            } 
        }

        public bool IsNew { get; set; }
        public bool IsModified { get; set; }

        public List<Colore> Colori
        {
            get { return m_colori; }
            set { m_colori = value; }
        }

        public List<PolvereExt> Polveri
        {
            get { return m_polveri; }
            internal set { m_polveri = value; }
        }

        public AssociaPolvereDialog()
        {
            InitializeComponent();

            if (!DesignMode)
            {
                LoadPolveri();
                LoadColori();

                m_colori = new List<Colore>();
                m_polveri = new List<PolvereExt>();
            }
        }

        public AssociaPolvereDialog(bool modified)
        {
            InitializeComponent();

            IsModified = modified;

            if (!DesignMode)
            {
                m_colori = new List<Colore>();
                m_polveri = new List<PolvereExt>();
            }
        }

        private void LoadColori()
        {
            ColoriFacade fcf = new ColoriFacade();
            fcf.RunAsync(() => fcf.GetAllEssential(), (result) =>
            {
                this.objectListViewColori.AddDecoration(new EditingCellBorderDecoration { UseLightbox = true });
                this.objectListViewColori.SetObjects((List<Colore>)result);
            });
        }

        private void LoadPolveri()
        {
            PolvereFacade fcf = new PolvereFacade();
            fcf.RunAsync(() => fcf.GetAllEssential(), (result) =>
            {
                this.objectListViewPolveri.AddDecoration(new EditingCellBorderDecoration { UseLightbox = true });
                this.objectListViewPolveri.SetObjects((List<PolvereExt>)result);
            });
        }

        private void ModifyFormula(ComposizioneColore2 formula)
        {
            ComposizioneColoreFacadeExt ccfext = new ComposizioneColoreFacadeExt();
            List<PolvereExt> m_polveriAssociate = ccfext.GetPolveriAssociate(formula.Colore.ID);

            this.objectListViewColori.Items.Clear();
            this.objectListViewPolveri.Items.Clear();

            List<Colore> m_colori = new List<Colore>();
            m_colori.Add(m_formula.Colore);
            this.objectListViewColori.SetObjects(m_colori);
            this.objectListViewPolveri.SetObjects(m_polveriAssociate);

            foreach (OLVListItem item in this.objectListViewColori.Items)
            {
                this.objectListViewColori.ToggleCheckObject(item.RowObject);
            }

            foreach (OLVListItem item in this.objectListViewPolveri.Items)
            {
                this.objectListViewPolveri.ToggleCheckObject(item.RowObject);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.btnOk.DialogResult = System.Windows.Forms.DialogResult.None;

            if (ValidateData())
            {
                foreach (Colore c in this.objectListViewColori.CheckedObjects)
                {
                    m_colori.Add(c);
                }

                foreach (PolvereExt p in this.objectListViewPolveri.CheckedObjects)
                {
                    if (p.Base && p.Venatura)
                    {
                        // caso particolre in cui la polvere è sua base che venatura...
                        p.Base = true;
                        p.Venatura = false;
                        m_polveri.Add(p);

                        PolvereExt p1 = (PolvereExt)p.Clone();
                        p1.Base = false;
                        p1.Venatura = true;
                        m_polveri.Add(p1);
                    }
                    else
                    {
                        m_polveri.Add(p);
                    }
                }

                //if (this.IsModified)
                //{
                    dataExt = new ComposizioneColoreExt();
                    dataExt.Colore = m_colori[0];
                    dataExt.Polveri = m_polveri;
                //}

                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                //this.btnOk.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                this.btnOk.DialogResult = System.Windows.Forms.DialogResult.None;
                MessageBox.Show("Verificare che le polveri nell'elenco siano definite come BASE o come VENATURA !", "Gestione Polveri", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private bool ValidateData()
        {
            bool retValue = true;

            foreach (PolvereExt polvere in this.objectListViewPolveri.CheckedObjects)
            {
                //PolvereExt polvere = (PolvereExt)item.RowObject;

                //if ((!polvere.Base && !polvere.Venatura) || (polvere.Base && polvere.Venatura))
                if (!polvere.Base && !polvere.Venatura)
                {
                    retValue = false;
                    break;
                }
            }

            return retValue;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtCerca_TextChanged(object sender, EventArgs e)
        {
            TextMatchFilter filter = null;
            if (!String.IsNullOrEmpty(txtCerca.Text))
            {
                filter = TextMatchFilter.Prefix(this.objectListViewPolveri, txtCerca.Text);

                objectListViewPolveri.AdditionalFilter = filter;

                //switch (matchKind)
                //{
                //    case 0:
                //    default:
                //        filter = TextMatchFilter.Contains(olv, txt);
                //        break;
                //    case 1:
                //        filter = TextMatchFilter.Prefix(olv, txt);
                //        break;
                //    case 2:
                //        filter = TextMatchFilter.Regex(olv, txt);
                //        break;
                //}
            }
            else
            {
                objectListViewPolveri.AdditionalFilter = null;
            }
        }

        private void txtCercaColore_TextChanged(object sender, EventArgs e)
        {
            TextMatchFilter filter = null;
            if (!String.IsNullOrEmpty(txtCercaColore.Text))
            {
                filter = TextMatchFilter.Prefix(this.objectListViewColori, txtCercaColore.Text);

                //this.objectListViewColori.ModelFilter = new OnlyEmergenciesFilter(txtCercaColore.Text);
                
                objectListViewColori.AdditionalFilter = filter;
            }
            else
            {
                objectListViewColori.AdditionalFilter = null;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NewPolvereElenco npe = new NewPolvereElenco();
            if (npe.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (npe.PolveriSelezionate.Count > 0)
                {
                    List<PolvereExt> m_polveri = new List<PolvereExt>();
                    foreach (OLVListItem item in this.objectListViewPolveri.Items)
                    {
                        m_polveri.Add((PolvereExt)item.RowObject);
                    }

                    m_polveri.AddRange(npe.PolveriSelezionate);

                    this.objectListViewPolveri.Items.Clear();
                    this.objectListViewPolveri.SetObjects(m_polveri);

                    foreach (OLVListItem item in this.objectListViewPolveri.Items)
                    {
                        this.objectListViewPolveri.ToggleCheckObject(item.RowObject);
                    }
                }
            }
        }

        
    }

    public class OnlyEmergenciesFilter : IModelFilter
    {
        private string m_textToSearch = string.Empty;

        public OnlyEmergenciesFilter(string textToSearch)
        {
            m_textToSearch = textToSearch;
        }

        public bool Filter(object modelObject)
        {
            return ((Colore)modelObject).CodiceColore.StartsWith(m_textToSearch);
        }
    }

}
